package cn.demoncat.util.web.constant;

import cn.demoncat.util.exception.RestRuntimeException;

import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

/**
 * HTTP常量
 * 
 * @author 延晓磊
 *
 * @since 2018年11月14日
 */
public class HttpConstant {

	/**
	 * 请求类型错误或发生冲突：只能有一种请求类型（FORM请求/文件上传请求、TEXT请求、HTML请求、JSON请求、XML请求、流请求），不能变更和混合
	 */
	public static final RestRuntimeException REQUEST_TYPE_ERROR = new RestRuntimeException("请求类型错误");

	/**
	 * HTTP请求日志
	 */
	public static final String HTTP_SEND_LOG = "发起HTTP请求：method = {}, url = {}";
	
	/**
	 * 请求方法
	 */
	public static final class Method{
		//GET
		public static final String GET = "GET";
		//POST
		public static final String POST = "POST";
		//PUT
		public static final String PUT = "PUT";
		//OPTIONS 跨域预请求
		public static final String OPTIONS = "OPTIONS";
		//DELETE
		public static final String DELETE = "DELETE";
	}

	/**
	 * 内容类型
	 */
	public static final class ContentType{
		public static final String FORM = "application/x-www-form-urlencoded;charset=UTF-8";
		public static final String MULTIPART = "multipart/form-data";
		public static final String HTML = "application/html;charset=UTF-8";
		public static final String TEXT = "text/plain;charset=UTF-8";
		public static final String JSON = "application/json;charset=UTF-8";
		public static final String XML = "application/xml;charset=UTF-8";
		public static final String BINARY = "application/octet-stream";
	}
	
	/**
	 * 请求类型
	 */
	public enum RequestType{
		// 表单参数
		FORM("10", ContentType.FORM, "表单"),
		MULTIPART("11", ContentType.MULTIPART , "文件上传"),
		// 请求体参数
		HTML("20", ContentType.HTML, "HTML"),
		TEXT("30", ContentType.TEXT, "文本"),
		JSON("31", ContentType.JSON, "JSON文本"),
		XML("32", ContentType.XML, "XML文本"),
		BINARY("40", ContentType.BINARY, "二进制流");
		
		/**
		 * 编号
		 */
		private final String code;
		/**
		 * 内容类型
		 */
		private final String contentType;
		/**
		 * 描述
		 */
		private final String description;
		
		/**
		 * 根据code获取
		 * 
		 * @param code
		 * @return
		 * 
		 * @author 延晓磊
		 *
		 * @since 2019年10月13日
		 */
		public static RequestType getByCode(String code) {
			for (RequestType type : RequestType.values()) {
				if (type.getCode().equals(code)) {
					return type;
				}
			}
			return null;
		}
		
		/**
		 * 根据名称获取
		 * 
		 * @param name
		 * @return
		 * 
		 * @author 延晓磊
		 *
		 * @since 2019年10月13日
		 */
		public static RequestType getByName(String name) {
			return RequestType.valueOf(name);
		}
		
		
		RequestType(String code, String contentType, String description) {
			this.code = code;
			this.contentType = contentType;
			this.description = description;
		}
		public String getCode() {
			return code;
		}
		public String getContentType() {
			return contentType;
		}
		public String getDescription() {
			return description;
		}
	}

	/**
	 * 默认的SSL证书信任管理器：不做校验
	 */
	public static final X509TrustManager DEFAULT_TRUST_MANAGER = new X509TrustManager() {
		@Override
		public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
		}

		@Override
		public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[0];
		}
	};
	
}
